package com.zhou.filter;

import com.zhou.utils.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter({"/admin/index.html"})
public class AccessControllerFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String userName = (String) req.getSession().getAttribute("username");
        if (userName!=null){
            chain.doFilter(request, response);//放行
        }else {
            resp.sendError(403,"很遗憾权限不够");
        }

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
